package com.by.data.insert;

import java.sql.*;

/**
 * <p>数据插入工具类<p/>
 *
 * @author boyin
 * @ClassName TestDataInsert
 * @Description 用于想数据库中插入4000w条数据做冷热分离处理
 * @date 2025/3/3 21:58
 */
public class TestDataInsertUnit {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        final Connection connection = DriverManager.getConnection("jdbc:mysql://8.152.205.167:3306/COLD_HOT_SEP?charcterEncoding=UTF-8&serverTimezone=UTC",
                "root", "root");
        final String sql = "INSERT INTO T2 (fname, lname, region_code,DOB) VALUES (?,?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Runnable r1 = () -> {
            for (int i = 0; i < 10000000; i++) {
                try {
                    Date date = new Date(1970 + i % 10, i % 12, i % 8);
                    preparedStatement.setString(1, "fr1" + i);
                    preparedStatement.setString(2, "lr1" + i);
                    preparedStatement.setInt(3, i%250);
                    preparedStatement.setDate(4,date);
                    preparedStatement.executeLargeUpdate();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
        Thread t1 = new Thread(r1);
        t1.start();
    }
}
